package com.jn.glide.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSON;
import com.jn.glide.R;
import com.jn.glide.adapter.TitlesAdapter;
import com.jn.glide.base.BaseActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OKHttpPicassoActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private static List<Entity.Data> mList;
    private static TitlesAdapter mAdapter;
    @SuppressLint("HandlerLeak")
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10) {
                mAdapter.updateData(mList);
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_avatar;
    }

    @Override
    public void init() {
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new TitlesAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(new TitlesAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Entity.Data ite) {
                Intent intent = new Intent(OKHttpPicassoActivity.this, ImagerActivity.class);
                intent.putExtra("url", ite.url);
                startActivity(intent);
            }
        });
        request();
    }

    public void request() {
//创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
        final Request request = new Request.Builder()
                .url("https://api.oioweb.cn/api/bing")
                .build();
//new call
        Call call = mOkHttpClient.newCall(request);
//请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.i("aaa", "这是请求成功的数据" + str);
                Entity entity = JSON.parseObject(str, Entity.class);
                if (200 == entity.code) {
                    mList = entity.result;
                    //这里是子线程不能更新UI
                    handler.sendEmptyMessage(10);
                } else {
                    System.out.println("请求失败");
                }
            }

        });
    }

    public class Entity {
        public int code;
        public String msg;
        public ArrayList<Data> result;

        public class Data {
            public String copyright;
            public String copyrightlink;
            public String title;
            public String url;
        }
    }
}
